package com.snail.clouds.framework.service;

import com.snail.clouds.framework.dto.UserLoginDTO;
import com.snail.clouds.framework.dto.UserLoginResponseDTO;
import com.snail.clouds.framework.entity.UserProfile;
import com.snail.clouds.framework.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 认证服务类
 */
@Slf4j
@Service
public class AuthService {
    
    @Autowired
    private UserProfileService userProfileService;
    
    @Autowired
    private JwtUtil jwtUtil;
    
    @Value("${jwt.expiration:86400}")
    private Long jwtExpiration;
    
//    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    
    /**
     * 用户登录
     * @param loginDTO 登录信息
     * @return 登录响应
     */
    public UserLoginResponseDTO login(UserLoginDTO loginDTO) {
        log.info("开始用户登录流程，用户编码: {}", loginDTO.getUserCode());
        
        // 根据邮箱查找用户
        UserProfile user = userProfileService.getByUserCode(loginDTO.getUserCode());
        if (user == null) {
            log.warn("用户不存在，用户编码: {}", loginDTO.getUserCode());
            throw new RuntimeException("用户不存在");
        }
        
        log.info("找到用户，用户ID: {}, 用户编码: {}", user.getId(), user.getUserCode());
        
        // 验证密码(不做加密验证)
        if (!Objects.equals(loginDTO.getPassword(), user.getPassword())) {
            log.warn("密码错误，用户编码: {}", loginDTO.getUserCode());
            throw new RuntimeException("密码错误");
        }
        
        log.info("密码验证通过，用户编码: {}", loginDTO.getUserCode());
        
        // 生成JWT Token
        String token = jwtUtil.generateToken(user.getId(), user.getUserCode());
        
        // 构建响应
        UserLoginResponseDTO response = new UserLoginResponseDTO()
                .setToken(token)
                .setUserId(user.getId())
                .setUsername(user.getUserCode())
                .setUserCode(user.getUserCode())
                .setNickname(user.getNickname())
                .setAvatar("") // 暂时使用空字符串，后续可以添加avatar字段
                .setExpiresIn(jwtExpiration);
        
        log.info("用户登录成功: {}", user.getUserCode());
        return response;
    }
    
    /**
     * 验证Token
     * @param token JWT Token
     * @return 是否有效
     */
    public boolean validateToken(String token) {
        return jwtUtil.validateToken(token);
    }
    
    /**
     * 从Token中获取用户ID
     * @param token JWT Token
     * @return 用户ID
     */
    public Long getUserIdFromToken(String token) {
        return jwtUtil.getUserIdFromToken(token);
    }
    
    /**
     * 从Token中获取用户名
     * @param token JWT Token
     * @return 用户名
     */
    public String getUsernameFromToken(String token) {
        return jwtUtil.getUsernameFromToken(token);
    }
    
    /**
     * 刷新Token
     * @param token 原始Token
     * @return 新的Token
     */
    public String refreshToken(String token) {
        return jwtUtil.refreshToken(token);
    }
}